package cc.rengu.igas.share.core.service.channel.wechat;

import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.WechatRealNameApplymentRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatRealNameApplymentResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.base.ChannelBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.ChannelCallModeEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

/**
 * 实名验证提交申请
 * Created by lzl on 2021/4/23 11:08
 */
public class WechatRealNameApplymentService extends ChannelBaseService {
    @Override
    public Object callPreProc(TxnInfo txnInfo, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        Header wxHeader = new Header();
        wxHeader.setInstId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID));
        wxHeader.setChanlId(channelCallCfg.getMsgSrcId());
        wxHeader.setSrcSysId(AppParamConstant.SYS_ID);
        wxHeader.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
        wxHeader.setVersion(ShareParamConstant.DUBBO_VSERSION);
        wxHeader.setTxnNum(channelCallCfg.getCallChannelTxn());
        wxHeader.setTraceNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM) + channelCallCfg.getCallIndex());
        wxHeader.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        wxHeader.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        WechatRealNameApplymentRequest wechatRealNameApplymentRequest = new WechatRealNameApplymentRequest();
        ConvertUtil.convertOutput(wechatRealNameApplymentRequest);

        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(wxHeader.getInstId(), xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.MCHNT_NO));
        wechatRealNameApplymentRequest.setHeader(wxHeader);
        wechatRealNameApplymentRequest.setChannelId(channelCallCfg.getCallChannelId());

        //todo 涉及字段比较多，考虑是否把请求实体存放到缓存
//        //商户产品信息表的微信支付通道编码
//        wechatRealNameApplymentRequest.setChannelType(wechatMchntInfo.getDstChannelType());
//        wechatRealNameApplymentRequest.setBusinessCode(sysSeqNum);
//
//        //联系人信息
//        ContactInfo contactInfo = new ContactInfo();
//        contactInfo.setCertId(mchntBaseInfo.getMchntContactCertNo());
//        contactInfo.setContactName(mchntBaseInfo.getMchntContactName());
//        contactInfo.setContactPhone(mchntBaseInfo.getMchntContactPhone());
//        wechatRealNameApplymentRequest.setContactInfo(contactInfo);
//        //主体信息
//        initSubjectInfo(wechatRealNameApplyRequest, mchntBaseInfo, wechatMchntInfo, mchntSignInfo, mchntImageInfoMapper, wechatRealNameApplymentRequest);
//
//        //法人信息
//        IdentificationInfo identificationInfo = new IdentificationInfo();
//        identificationInfo.setCertNo(mchntSignInfo.getArtifCertNo());
//        identificationInfo.setCertType(WechatCertTypeEnum.getWechatCertTypeEnumByType(mchntSignInfo.getArtifCertType()));
//        identificationInfo.setName(mchntSignInfo.getArtifName());
//        //正面照
//        identificationInfo.setFrontImageId(artifFaceCertImage.getWechatMediaId());
//        // 示例值：[\"1970-01-01\",\"forever\"]
//        String validDate = convertToWechatValidDate(mchntSignInfo.getCertEffectiveDate(), mchntSignInfo.getCertExpiryDate());
//        identificationInfo.setValidDate(validDate);
//        wechatRealNameApplymentRequest.setIdentificationInfo(identificationInfo);
//
//        //更新实名认证业务单号 为请求traceNo
//        wechatMchntInfo.setRealNameBizNo(header.getTraceNo());

        //TODO  businessCode;  contactInfo;   subjectInfo;  identificationInfo;  channelType;  赋值
        rglog.debug("实名验证提交申请请求:<{}>", JSON.toJSONString(wechatRealNameApplymentRequest));

        return wechatRealNameApplymentRequest;
    }

    @Override
    public PayOrderInfo generatePayOrderInfo(ChannelCallCfg channelCallCfg, OrderInfo orderInfo, Object request) throws Exception {
        return null;
    }

    @Override
    public Object callProc(ChannelCallCfg channelCallCfg, Object request) throws Exception {
        WechatRealNameApplymentRequest wechatRealNameApplymentRequest = (WechatRealNameApplymentRequest) request;
        /* Step1：调用接口，获取结果 */
        Result<WechatRealNameApplymentResponse> callResult;
        DubboService dubboService = new DubboServiceImpl();
        if (AppParamConstant.YES.equals(channelCallCfg.getSyncCallFlag())) {
            /* Step1.1：异步调用 */
            int iReturnCode;
            int callTimes = 0 == channelCallCfg.getSyncQueryRepeatNum() ? 1 : channelCallCfg.getSyncQueryRepeatNum();
            int gatTime = channelCallCfg.getSyncQueryRepeatTime();
            int gatType = StringUtil.isEmptyOrNull(channelCallCfg.getSyncQueryGapType()) ? 0 : Integer.parseInt(channelCallCfg.getSyncQueryGapType());
            if (ChannelCallModeEnum.LOCAL.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.1.1：异步应用内调用 */
                Class clasz = Class.forName(ShareServiceConstant.WECHAT_MCHNT_SERVICE_INNER_PATH);
                iReturnCode = dubboService.asynCallDubboInnerMethod(clasz, ShareServiceConstant.WECHAT_REAL_NAME_APPLYMENT_METHOD, wechatRealNameApplymentRequest, channelCallCfg.getSyncResultServiceName(), callTimes, gatType, gatTime);
            } else if (ChannelCallModeEnum.REMOTE.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.1.2：异步远程调用 */
                iReturnCode = dubboService.asynCallDubboService(ShareServiceConstant.WECHAT_MCHNT_SERVICE, ShareServiceConstant.WECHAT_REAL_NAME_APPLYMENT_METHOD, wechatRealNameApplymentRequest, channelCallCfg.getSyncResultServiceName(), callTimes, gatType, gatTime);
            } else {
                rglog.error("不支持的通道服务调用方式:{}，交易拒绝!", channelCallCfg.getChannelCallMode());
                throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR);
            }
            if (0 == iReturnCode) {
                return new BizResponse<>(OltpRpcdEnum.TRANS_SUCCESS.getRespCode(), OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            } else {
                return new BizResponse<>(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            }
        } else {
            /* Step1.2：同步调用 */
            if (ChannelCallModeEnum.LOCAL.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.2.1：同步应用内调用 */
                Class clasz = Class.forName(ShareServiceConstant.WECHAT_MCHNT_SERVICE_INNER_PATH);
                callResult = (Result<WechatRealNameApplymentResponse>) dubboService.callDubboInnerMethod(clasz, ShareServiceConstant.WECHAT_REAL_NAME_APPLYMENT_METHOD, wechatRealNameApplymentRequest);
            } else if (ChannelCallModeEnum.REMOTE.getMode().equals(channelCallCfg.getChannelCallMode())) {
                /* Step1.2.2：同步远程调用 */
                callResult = (Result<WechatRealNameApplymentResponse>) dubboService.callDubboService(ShareServiceConstant.WECHAT_MCHNT_SERVICE, ShareServiceConstant.WECHAT_REAL_NAME_APPLYMENT_METHOD, wechatRealNameApplymentRequest);
            } else {
                rglog.error("不支持的通道服务调用方式:{}，交易拒绝!", channelCallCfg.getChannelCallMode());
                throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR);
            }
        }
        return callResult;
    }

    @Override
    public Object querySyncCallResult(PayOrderInfo payOrderInfo, Object request) {
        return null;
    }

    @Override
    public OrderInfo callResultOrderProc(OrderInfo orderInfo, String payOrderIndex, TransStatusEnum transStatusEnum, Object response) {
        return null;
    }

}
